#include "deleteque.h"
#include "ui_deleteque.h"

Deleteque::Deleteque(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Deleteque)
{
    ui->setupUi(this);
    this->setWindowTitle("删除试题");
    db = Database::getInstance().getDatabase();
}

Deleteque::~Deleteque()
{
    delete ui;
}

void Deleteque::on_pushButton_del_exit_clicked()
{
    emit del_exit();
}

void Deleteque::on_pushButton_refresh_clicked()
{
    QString sql = "select * from questions";
    QSqlQuery query(db);
    if(!query.exec(sql))
        QMessageBox::information(this,"查看","Error"+db.lastError().text());
    int line_count = ui->tableWidget->rowCount();
    //清空表格
    for(int i=0;i<line_count;i++)
        ui->tableWidget->removeRow(0);
    while(query.next())
    {
        qDebug() << query.value(0).toString() << query.value(1).toString();
        ui->tableWidget->insertRow(0);//插入0行
        QTableWidgetItem *item0 = new QTableWidgetItem(query.value(0).toString());
        QTableWidgetItem *item1 = new QTableWidgetItem(query.value(1).toString());
        QTableWidgetItem *item2 = new QTableWidgetItem(query.value(2).toString());
        ui->tableWidget->setItem(0,0,item0);
        ui->tableWidget->setItem(0,1,item1);//设置item
        ui->tableWidget->setItem(0,2,item2);
    }
}

void Deleteque::on_pushButton_delete_conf_clicked()
{
    int id = ui->lineEdit_que_id->text().toInt();
    question Myquestion(0,"","");
    if(Myquestion.deleteQuestion(id)){
        QMessageBox::information(this, "Success", "Question delete successfully!");
    }else {
        QMessageBox::critical(this, "Error", "Failed to delete question.");
    }

}
